package czopyk.model.persistence.actions;

import java.math.BigDecimal;

import model.Time;
import czopyk.model.persistence.ValueExtractor;

public class StopAction extends CommonAction implements Action {

	private final String var, value, c;

	public StopAction(String var, String value, String c, ValueExtractor ve) {
		super(null, ve);
		this.var = var;
		this.value = value;
		this.c = c;
	}

	public void perform() {
		BigDecimal var_ = ve.getNumber(var), value_ = ve.getNumber(value);
		if (var_ == null) {
			throw new IllegalArgumentException("Variable '" + var + "' not set");
		}
		if (value_ == null) {
			throw new IllegalArgumentException("Value '" + value + "' not defined");
		}
		if (">".equals(c)) {
			if (var_.compareTo(value_) > 0) {
				Time.stop();
			}
		} else if ("<".equals(c)) {
			if (var_.compareTo(value_) < 0) {
				Time.stop();
			}
		} else if (">=".equals(c)) {
			if (var_.compareTo(value_) >= 0) {
				Time.stop();
			}
		} else if ("<=".equals(c)) {
			if (var_.compareTo(value_) <= 0) {
				Time.stop();
			}
		} else {
			throw new IllegalArgumentException("Unknown operator: " + c);
		}
	}
	
}
